<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$str = file_get_contents('php://input');
include '../login/db_connect.php';
include '../login/functions.php';
sec_session_start();
if (isset ( $_COOKIE ['login_string'] )) {
	$login_string = $_COOKIE ['login_string'];
	$username = $_COOKIE ['username'];
	$user_id = $_COOKIE ['user_id'];

	$_SESSION ['user_id'] = $user_id;
	$_SESSION ['login_string'] = $login_string;
	$_SESSION ['username'] = $username;
}

if ((login_check ( $mysqli ) == true)&&(empty($str))){
	$vendors = array();
	$query_all_vendors="SELECT appellative,id_vendor FROM vendors ORDER BY appellative";
	if ($result = $mysqli->query ($query_all_vendors )) {
		while ( $row = $result->fetch_assoc () ) {
			$vendors[]=$row;
		}
		$result->free();
	}
	$to_send=json_encode($vendors);
	echo $to_send;
}
else if (login_check ( $mysqli ) == true){	
		$products = array();
		$query_products="SELECT * FROM catalog WHERE vendor=".$_POST['id'];
		if ($result = $mysqli->query ($query_products)) {
			while ( $row = $result->fetch_assoc () ) {
				$products[]=$row;
			}
			$result->free();
		}
		else $products=array();
		$to_send=json_encode($products);
		echo $to_send;
}

?>